/*
 * @Descripttion:
 * @LastEditors: xueyc
 * @LastEditTime: 2024-08-05 20:42:44
 */
import { defineConfig } from 'vite'
import path from 'path'
import vue from '@vitejs/plugin-vue'
import { createSvg } from './src/assets/icons'
//自动按需导入
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
//按需引入
import { createStyleImportPlugin, VxeTableResolve } from 'vite-plugin-style-import'
//开启gzip、br压缩
import compressPlugin from 'vite-plugin-compression'
//打包分析
import { visualizer } from 'rollup-plugin-visualizer'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    createSvg('./src/assets/icons/svg/'),
    //自动按需导入
    AutoImport({
      resolvers: [ElementPlusResolver()]
    }),
    Components({
      resolvers: [ElementPlusResolver()]
    }),
    //按需引入[vxetable]
    createStyleImportPlugin({
      resolves: [VxeTableResolve()]
    }),
    //开启gzip、br压缩
    compressPlugin({
      ext: '.gz', //gz br
      algorithm: 'gzip', //brotliCompress gzip
      deleteOriginFile: false
    }),
    //打包分析
    visualizer({
      open: true,
      //注意这里要设置为true,否则无效
      filename: 'dist/stats.html', //分折图生成的文件名
      gzipSize: true, //收集gzip大小并将其显示
      brotliSize: true //收集brotli大小并将其显示
    })
  ],
  //解析别名
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src'),
      '@store': path.resolve(__dirname, 'src/store'),
      '@storeAction': path.resolve(__dirname, 'src/utils/cache/store'),
      '@constants': path.resolve(__dirname, 'src/utils/constants'),
      '@methods': path.resolve(__dirname, 'src/utils/methods'),
      '@typings': path.resolve(__dirname, 'src/utils/typings'),
      '@dataview/datav-vue3': path.resolve(__dirname, 'node_modules/@dataview/datav-vue3/es/index.mjs')
    }
  },
  server: {
    open: false,
    host: '0.0.0.0',
    port: 5176,
    proxy: {
      // 代理网址
      '/api': {
        target: 'http://localhost:9088',
        changeOrigin: true,
        rewrite: path => path.replace('/api/', '')
      }
    }
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "./src/styles/theme/index.scss" as *;`
      }
    },
    //[WARNING] "@charset" must be the first rule in the file
    postcss: {
      plugins: [
        {
          postcssPlugin: 'internal:charset-removal',
          AtRule: {
            charset: atRule => {
              if (atRule.name === 'charset') {
                atRule.remove()
              }
            }
          }
        }
      ]
    }
  }
})
